import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.utils.StringUtils;

/**
 * 解析美宝莲产品信息
 * 
 * @author asus
 *
 */
public class SpliderMaybellineChinaGoods extends UDF {
	
	 /* public static void main(String args[]) {
		  String html = TestHtml.html();
	  evaluate(html); }*/
	 

	public String evaluate(String html) {
		JSONObject obj = new JSONObject();
		if (StringUtils.isNotBlank(html)) {
			Document document = Jsoup.parse(html);
			// 获取商品名称
			Elements titleClass = document.getElementsByClass("product-info__title");
			if (null != titleClass && titleClass.size() != 0) {
				Element titleElement = titleClass.get(0);
				String text = titleElement.text();
				String colorText = titleElement.getElementsByClass("product-info__shade-name").text();
				String title = text.replace(colorText, "").trim();
				obj.put("cnTitle", title);
			}
			Elements titleClass1 = document.getElementsByClass("fpw__text-box");
			if (null != titleClass1 && titleClass1.size() != 0) {
				String text = titleClass1.get(0).getElementsByTag("strong").text();
				obj.put("enTitle", text);
			}

			// 获取产品分类
			Elements categoryClass = document.getElementsByClass("icon icon-arrow_right");
			if (null != categoryClass && categoryClass.size() != 0) {
				String category = categoryClass.get(0).text();
				obj.put("category", category);
			}

			// 获取商品颜色
			Elements colorClass = document.getElementsByClass("product-info__primary-swatches color-picker");
			Elements newColorClass = document.getElementsByClass("product-info__new-shades color-picker");
			if (null != colorClass && colorClass.size() != 0) {
				Elements colorTag = colorClass.get(0).getElementsByTag("li");
				String color = "";
				if (null != colorTag && colorTag.size() != 0) {
					
					StringBuffer sb = new StringBuffer();
					for (Element e : colorTag) {
						String dataColor = e.attr("data-color");
						sb = sb.append(dataColor).append(",");
					}
					color = sb.toString();
					if (color.contains(",")) {
						color = color.substring(0, color.lastIndexOf(","));
					}

				} else {
					Elements newColorTag = newColorClass.get(0).getElementsByTag("li");
					StringBuffer sb = new StringBuffer();
					for (Element e : newColorTag) {
						String dataColor = e.attr("data-color");
						sb = sb.append(dataColor).append(",");
					}
					color = sb.toString();
					if (color.contains(",")) {
						color = color.substring(0, color.lastIndexOf(","));
					}
				}
				obj.put("color", color);
			}

			// 获取产品价格 product-info__price
			Elements priceClass = document.getElementsByClass("product-info__price");
			if (null != priceClass && priceClass.size() != 0) {
				String text = priceClass.get(0).text();
				String price = text.replace("建议零售价", "").trim();
				obj.put("price", price);
			}

			// 获取产品信息
			Elements infoClass = document.getElementsByClass("product-info__copy");
			if (null != infoClass && infoClass.size() != 0) {
				String info = infoClass.text();
				obj.put("info", info);
			}
			Elements descriptClass = document.getElementsByClass("cheatsheet__fact");
			if (null != descriptClass && descriptClass.size() == 3) {
				String descript = descriptClass.get(0).text();
				descript = descript.replace("产品介绍", "");
				obj.put("descript", descript);

				String function = descriptClass.get(1).text();
				function = function.replace("功效", "");
				obj.put("function", function);

				String howUse = descriptClass.get(2).text();
				howUse = howUse.replace("使用方式", "");
				obj.put("howUse", howUse);

			} else {

			}
			System.out.println(JSON.toJSONString(obj));
			return JSON.toJSONString(obj);
		}
		return null;
	}
}
